package com.peaker.advertisingpro.view

import android.content.Context
import android.os.Build
import android.text.TextUtils
import android.util.AttributeSet
import android.view.Gravity
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.widget.FrameLayout
import tv.danmaku.ijk.media.player.IMediaPlayer
import tv.danmaku.ijk.media.player.IjkMediaPlayer
import java.io.IOException


/**
 *  @ 作者 CODE_LYF
 *  @ 时间   2021/3/28.
 *  代码人生
 */
class IjkVideoView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
    private var mContext //上下文
            : Context? = null
    private var mMediaPlayer: IjkMediaPlayer? = null //视频控制类

        private var mVideoPlayerListener : VideoPlayerListener? = null
    private var mSurfaceView //播放视图
            : SurfaceView? = null
    private var mPath = "" //视频文件地址

    abstract class VideoPlayerListener : IMediaPlayer.OnPreparedListener,
        IMediaPlayer.OnCompletionListener,
        IMediaPlayer.OnErrorListener


    init {
        initVideoView(context)
        mContext=context
    }

    private fun initVideoView(context: Context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            focusable=FOCUSABLE_AUTO
        }
    }

    fun setPath(path: String) {
        if (TextUtils.equals("", mPath)) {
            mPath = path
            initSurfaceView()
        } else {
            mPath = path
            loadVideo()
        }
    }

    private fun initSurfaceView() {
        mSurfaceView = SurfaceView(mContext)
        mSurfaceView!!.getHolder().addCallback(LmnSurfaceCallback())
        val layoutParams = LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT,
            Gravity.CENTER
        )
        mSurfaceView!!.setLayoutParams(layoutParams)
        this.addView(mSurfaceView)
    }

    //surfaceView的监听器
    inner class LmnSurfaceCallback : SurfaceHolder.Callback {
        override fun surfaceCreated(holder: SurfaceHolder) {

        }
        override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
            loadVideo()
        }

        override fun surfaceDestroyed(holder: SurfaceHolder) {

        }
    }

    //加载视频
    private fun loadVideo() {
        if (mMediaPlayer != null) {
            mMediaPlayer!!.stop()
            mMediaPlayer!!.release()
        }
        val ijkMediaPlayer = IjkMediaPlayer()
        mMediaPlayer = ijkMediaPlayer
        if (mVideoPlayerListener != null) {
            mMediaPlayer!!.setOnPreparedListener(mVideoPlayerListener)
            mMediaPlayer!!.setOnErrorListener(mVideoPlayerListener)
        }
        try {
            mMediaPlayer!!.setDataSource(mPath)
        } catch (e: IOException) {
            e.printStackTrace()
        }
        mMediaPlayer!!.setDisplay(mSurfaceView!!.holder)
        mMediaPlayer!!.prepareAsync()
    }

    fun setListener(listener: VideoPlayerListener) {
        this.mVideoPlayerListener = listener
        if (mMediaPlayer != null) {
            mMediaPlayer!!.setOnPreparedListener(listener)
        }
    }
    fun start() {
        if (mMediaPlayer != null) {
            mMediaPlayer!!.start()
        }
    }

    fun pause() {
        if (mMediaPlayer != null) {
            mMediaPlayer!!.pause()
        }
    }

    fun stop() {
        if (mMediaPlayer != null) {
            mMediaPlayer!!.stop()
        }
    }

    fun reset() {
        if (mMediaPlayer != null) {
            mMediaPlayer!!.reset()
        }
    }

    fun release() {
        if (mMediaPlayer != null) {
            mMediaPlayer!!.reset()
            mMediaPlayer!!.release()
            mMediaPlayer = null
        }
    }


}